package com.rock.code.common.util;


import org.snmp4j.*;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.*;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SnmpClientV3 extends SnmpClient {

	@Override
	public SnmpClient connect() throws IOException {
		Address targetAddress = GenericAddress.parse("udp:" + host + "/" + port);
		TransportMapping transport = new DefaultUdpTransportMapping();
		this.snmp = new Snmp(transport);
		transport.listen();

		USM usm = new USM(SecurityProtocols.getInstance(),
				new OctetString(MPv3.createLocalEngineID()), 0);
		SecurityModels.getInstance().addSecurityModel(usm);

//		System.out.println("serurityName=="+serurityName);
//		System.out.println("authenticationProtocol=="+authenticationProtocol);
//		System.out.println("authenticationPassphrase=="+authenticationPassphrase);
//		System.out.println("securityLevel=="+securityLevel);
//		System.out.println("privacyProtocol=="+privacyProtocol);
//		System.out.println("privacyPassphrase=="+privacyPassphrase);
//		System.out.println("version=="+version);

		this.snmp.getUSM().addUser(
				serurityName,
				new UsmUser(serurityName,
						authenticationProtocol, authenticationPassphrase,
						privacyProtocol, privacyPassphrase));

		UserTarget usertarget = new UserTarget();
		usertarget.setVersion(version);
		usertarget.setAddress(targetAddress);
		usertarget.setSecurityLevel(securityLevel);
		usertarget.setSecurityName(serurityName);
		usertarget.setTimeout(3000);
		usertarget.setRetries(1);
		target = usertarget;

		return this;
	}

	@Override
	protected PDU createPDU() {
		return createPDU(PDU.GET);
	}

	@Override
	protected PDU createPDU(int type) {
		PDU pdu = new ScopedPDU();
		pdu.setType(type);
		return pdu;
	}

	public static void main(String[] args) throws Exception {
		List<String> list = new ArrayList<String>();
		list.add("20.42.128.30");
//		list.add("20.42.128.54");
//		list.add("20.42.128.42");
//		list.add("20.42.128.46");
//		list.add("20.42.0.1");
		for(int i = 0;i<list.size();i++){
			String ip = list.get(i).toString();
			System.out.println("开始"+ip);
			SnmpClient snmp = SnmpClient.create("3")
					.setHost(ip)
					.setPort("161")
					.setSerurityName("jbdlnewuser")
					.setAuthenticationProtocol("MD5")
					.setAuthenticationPassphrase("md5@jbdl")
//					.setPrivacyProtocol(null)
					.setSecurityLevel("authNoPriv");

			try {
				snmp.connect();
//				Map<String, String> s1 = snmp.walk(".1.3.6.1.2.1.17.2.6.0");
//				Map<String, String> s2 = snmp.walk(".1.3.6.1.2.1.17.2.7.0");
//				Map<String, String> s3 = snmp.walk(".1.3.6.1.2.1.17.1.2.0");
//				System.out.println("s1"+s1);
//				System.out.println("s2"+s2);
//				System.out.println("s3"+s3);
				snmp.walkIfTable("2","1");
				snmp.walkIfTable("1","2");
			} finally {
				snmp.close();
			}
			System.out.println("结束"+ip);
		}
	}
}
